package com.home.base.util;


import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author chenxf
 * @date 3/16/2018
 * @description
 */
public class DateUtil {

    public static LocalDate getLocalDateByDate(Date date){
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

   public static Date getByLocalDate(LocalDate localDate){
     ZoneId zoneId = ZoneId.systemDefault();
     ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
     return Date.from(zdt.toInstant());
   }


   public  static Date getDate(String dateStr){
     ZoneId zoneId = ZoneId.systemDefault();
     ZonedDateTime zdt = getLocalDate(dateStr).atStartOfDay(zoneId);
     return Date.from(zdt.toInstant());
   }
    public static LocalDate getLocalDate(String dateStr,String regex){
        String[] datas=dateStr.split(regex);
        return  LocalDate.of(Integer.parseInt(datas[0]),Integer.parseInt(datas[1]),Integer.parseInt(datas[2]));
    }

   public static LocalDate getLocalDate(String dateStr){
     String[] datas=dateStr.split("-");
     return  LocalDate.of(Integer.parseInt(datas[0]),Integer.parseInt(datas[1]),Integer.parseInt(datas[2]));
   }

  public static boolean localDateBeforeNow(String dateStr){
    return  LocalDate.now().isAfter(getLocalDate(dateStr));
  }

  public static Date getEDate(String dateStr){
    LocalDateTime localDateTime = LocalDateTime.parse(dateStr,DateTimeFormatter.ofPattern("MMM-dd-yyyy hh:mm:ss a", Locale.ENGLISH));
    ZoneId zoneId = ZoneId.of("UTC");
    return Date.from( localDateTime.atZone(zoneId).toInstant());
  }

  public static String getShortTime(Date date) {
    String shortString = null;
    long now = Calendar.getInstance().getTimeInMillis();
    if (date == null) {
      return shortString;
    }
    long delTime = (now - date.getTime()) / 1000;
    if (delTime > 365 * 24 * 60 * 60) {
      shortString = (int) (delTime / (365 * 24 * 60 * 60)) + "年前";
    } else if (delTime > 24 * 60 * 60) {
      shortString = (int) (delTime / (24 * 60 * 60)) + "天前";
    } else if (delTime > 60 * 60) {
      shortString = (int) (delTime / (60 * 60)) + "小时前";
    } else if (delTime > 60) {
      shortString = (int) (delTime / (60)) + "分前";
    } else if (delTime > 1) {
      shortString = delTime + "秒前";
    } else {
      shortString = "1秒前";
    }
    return shortString;
  }

  public static String formatDate(final Date date, final String format) {

    if (date == null){
        return "";
    }
    final SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    return dateFormat.format(date);
  }


}
